package com.marvels.financial.loan.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.marvels.financial.loan.dto.ReplacePaymentConditionDto;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.LendPaymentConditionDTO;
import com.marvels.financial.loan.dto.LendPaymentPlanDTO;
import com.marvels.financial.loan.dto.McfeRepaymentFlowDTO;
import com.marvels.financial.loan.service.LendPaymentApi;

/**
 * 放贷还款信息
 * @author tangpw
 *
 */
@RequestMapping("financial/loan/loanPayment/service")
@RestController
public class LendPaymentController {



	@Autowired
	private LendPaymentApi lendPaymentApi;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * 还款计划查询
	 * @param paymentConditionDTO
	 * @return
	 */
	@RequestMapping("paymentPlanList")
	@ResponseBody
	public MarvelsResultDTO paymentPlanList(@RequestBody LendPaymentConditionDTO paymentConditionDTO){
		try {
			Map<String, Object> map=lendPaymentApi.paymentPlanList(paymentConditionDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), map);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款计划查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款计划查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), ">>>>>>>>>>放贷还款信息-还款计划查询异常", null);
		}
	}


	@RequestMapping("extensionApply")
	@ResponseBody
	public MarvelsResultDTO extensionApply(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto){
		try {
			lendPaymentApi.extensionApply(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>还款计划展期FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>还款计划展期异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), ">>>>>>>>>>还款计划展期异常", null);
		}
	}


	/**
	 * 下载还款计划数据
	 * @param loanAuditConditionDto
	 */
	@RequestMapping("downLoadPlanList")
	public void downLoadPlanList(LendPaymentConditionDTO paymentConditionDTO,HttpServletRequest request,HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		OutputStream outs = null;
		try {
			response.setHeader("Content-disposition", "attachment; filename="+ new String("还款计划数据一览表.xls".getBytes("UTF-8"),"ISO8859-1"));
			HSSFWorkbook downLoadDeal2 = lendPaymentApi.downPaymentPlan(paymentConditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			downLoadDeal2.write(outs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载还款计划数据", e);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载还款计划数据", e);
		}finally{
			try {
				outs.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}

		}
	}



	/**
	 * 还款明细查询
	 * @param paymentConditionDTO
	 * @return
	 */
	@RequestMapping("paymentFlowList")
	@ResponseBody
	public MarvelsResultDTO paymentFlowList(@RequestBody LendPaymentConditionDTO paymentConditionDTO){
		try {
			return lendPaymentApi.paymentFlowList(paymentConditionDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), ">>>>>>>>>>放贷还款信息-还款明细查询异常", null);
		}
	}

	@RequestMapping("newPaymentFlowList")
	@ResponseBody
	public MarvelsResultDTO newPaymentFlowList(@RequestBody LendPaymentConditionDTO paymentConditionDTO){
		try {
			return lendPaymentApi.newPaymentFlowList(paymentConditionDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), ">>>>>>>>>>放贷还款信息-还款明细查询异常", null);
		}
	}

	@RequestMapping("newPaymentFlowDetail")
	@ResponseBody
	public MarvelsResultDTO newPaymentFlowDetail(@RequestBody LendPaymentConditionDTO paymentConditionDTO){
		try {
			return lendPaymentApi.newPaymentFlowDetail(paymentConditionDTO);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细详情FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细详情异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), ">>>>>>>>>>放贷还款信息-还款明细详情异常", null);
		}
	}

	/**
	 * 下载还款明细
	 * @param loanAuditConditionDto
	 */
//	@RequestMapping("downLoadFlowList")
	public void downLoadFlowListbak(LendPaymentConditionDTO paymentConditionDTO,HttpServletRequest request,HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		OutputStream outs = null;
		try {
			response.setHeader("Content-disposition", "attachment; filename="+ new String("还款明细数据一览表.xls".getBytes("UTF-8"),"ISO8859-1"));
			HSSFWorkbook downLoadDeal2 = lendPaymentApi.downPaymentFlow(paymentConditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			downLoadDeal2.write(outs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载还款计划数据", e);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载还款计划数据", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@RequestMapping("downLoadFlowList")
	public void downLoadFlowList(LendPaymentConditionDTO paymentConditionDTO,HttpServletRequest request,HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		OutputStream outs = null;
		try {
			response.setHeader("Content-disposition", "attachment; filename="+ new String("还款明细数据一览表.xls".getBytes("UTF-8"),"ISO8859-1"));
			HSSFWorkbook downLoadDeal2 = lendPaymentApi.newDownPaymentFlow(paymentConditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			downLoadDeal2.write(outs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载还款计划数据", e);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载还款计划数据", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	/**
	 * 代扣明细查询
	 * @param paymentConditionDTO
	 * @return
	 */
	@RequestMapping("withholdDetailList")
	@ResponseBody
	public MarvelsResultDTO withholdDetailList(@RequestBody LendPaymentConditionDTO paymentConditionDTO){
		try {
			Page<McfeRepaymentFlowDTO> list=lendPaymentApi.withholdDetailList(paymentConditionDTO);
					return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>放贷还款信息-还款明细查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), ">>>>>>>>>>放贷还款信息-还款明细查询异常", null);
		}
	}

	/**
	 * 下载代扣明细
	 * @param loanAuditConditionDto
	 */
	@RequestMapping("downLoadWithholdDetail")
	public void downLoadWithholdDetail(LendPaymentConditionDTO paymentConditionDTO,HttpServletRequest request,HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		OutputStream outs = null;
		try {
			response.setHeader("Content-disposition", "attachment; filename="+ new String("代扣明细数据一览表.xls".getBytes("UTF-8"),"ISO8859-1"));
			HSSFWorkbook downLoadDeal2 = lendPaymentApi.downLoadWithholdDetail(paymentConditionDTO);
			outs = response.getOutputStream();// 获取文件输出IO流
			downLoadDeal2.write(outs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载代扣明细数据", e);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载代扣明细数据", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
